<template>
  <div class="my-tag">
    <input 
      class="input" 
      type="text" 
      placeholder="输入标签" 

      v-if="emit"
      v-model="tag"
      v-select
      @blur="changeTag"
      @keyup.enter="changeTag"
    />
    <div 
      class="text"

      v-else
      @dblclick="changeInput"
    >{{ value }}</div>
  </div>
</template>

<script>
export default {
  name: 'HmTag',

  props: {
    value: {
      type: [String, Number],
      required: true
    }
  },

  data() {
    return {
      emit: false,
      tag: ''
    }
  },

  methods: {
    // 双击切换输入框
    changeInput() {
      this.emit = true

      this.tag = this.value
    },

    // 失焦隐藏输入框,并传给父组件数据
    changeTag() {
      this.emit = false

      this.$emit('input', this.tag)
    }
  },
}
</script>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
